﻿using DocumentFormat.OpenXml.Lite.Shared;
using DocumentFormat.OpenXml.Lite.Xml;
using DocumentFormat.OpenXml.Lite.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using DocumentFormat.OpenXml.Lite.Core;

namespace DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Style
{
    [XmlRoot("docDefaults", Namespace = Constants.WordMain)]
    public class DefaultStyles : XmlSerializable
    {
        [XmlElement("rPrDefault", Namespace = Constants.WordMain)]
        public DefaultRunProperties DefRunProperties { get; set; }

        [XmlElement("pPrDefault", Namespace = Constants.WordMain)]
        public DefaultParagraphProperties DefParagraphProperties { get; set; }

        public DefaultStyles() 
        {
        }

        /// <summary>
        /// Hint: you cannot add two elements with the same type.
        /// </summary>
        /// <param name="element"></param>
        /// <returns>True, if the list not contains element with the same type. 
        /// False otherwise (in this case the content will be override the old one).</returns>
        public bool AddRunProperty(IRunPropElement element)
        {
            if (DefRunProperties == null)
            {
                DefRunProperties = new DefaultRunProperties();
            }

            var obj = DefRunProperties.RunProperties.Properties
                .FirstOrDefault(e => e.GetType() == element.GetType());
            if (obj != null)
            {
                (obj as IRunPropElement).Copy(element);
            }
            else
            {
                DefRunProperties.RunProperties.Properties.Add(element);
                return true;
            }

            return false;
        }

        /// <summary>
        /// Hint: you cannot add two elements with the same type.
        /// </summary>
        /// <param name="element"></param>
        /// <returns>True, if the list not contains element with the same type. 
        /// False otherwise (in this case the content will be override the old one).</returns>
        public bool AddParagraphProperty(IParagraphPropElement element)
        {
            if (DefParagraphProperties == null)
            {
                DefParagraphProperties = new DefaultParagraphProperties();
            }

            var obj = DefParagraphProperties.ParagraphProperties.Properties
                .FirstOrDefault(e => e.GetType() == element.GetType());
            if (obj != null)
            {
                (obj as IRunPropElement).Copy(element);
            }
            else
            {
                DefParagraphProperties.ParagraphProperties.Properties.Add(element);
                return true;
            }

            return false;
        }
    }

    [XmlRoot("rPrDefault", Namespace = Constants.WordMain)]
    public class DefaultRunProperties : XmlSerializable
    {
        /// <summary>
        /// Hint: you cannot add two elements with the same type.
        /// </summary>
        [XmlElement("rPr", Namespace = Constants.WordMain)]
        public Base.RunProperties RunProperties { get; set; }

        public DefaultRunProperties() 
        {
            RunProperties = new Base.RunProperties();
        }
    }

    [XmlRoot("pPrDefault", Namespace = Constants.WordMain)]
    public class DefaultParagraphProperties : XmlSerializable
    {
        /// <summary>
        /// Hint: you cannot add two elements with the same type.
        /// </summary>
        [XmlElement("pPr", Namespace = Constants.WordMain)]
        public Base.ParagraphProperties ParagraphProperties { get; set; }

        public DefaultParagraphProperties()
        {
            ParagraphProperties = new Base.ParagraphProperties();
        }
    }
}
